#include<queue> #include<vector> #include<cstdio> #include<algorithm> using namespace std; const int Max = 21000000; typedef pair<int, int> Pair; int dis[300], head[300]; int S, T, m, n, tail; priority_queue< Pair, vector<Pair>, greater<Pair> > Q; struct data { int w, to, next; }edge[3000], k; void build(int a, int b, int x) { edge[tail].w = x; edge[tail].to = b; edge[tail].next = head[a]; head[a] = tail++; } void Dijkstra() { Pair s; int v, val; dis[S] = 0; s.first = 0; s.second = S; Q.push(s); while (!Q.empty()) { Pair u = Q.top(); Q.pop(); val = u.first; v = u.second; if (dis[v]<val) continue; for (int i = head[v]; i != -1; i = edge[i].next) { k = edge[i]; if (dis[k.to]>dis[v] + k.w) { dis[k.to] = dis[v] + k.w; Q.push(Pair(dis[k.to], k.to)); } } } } int main() { int a, b, x; while (scanf("%d%d", &n, &m) != EOF) { for (int i = 0; i<300; i++) dis[i] = Max; for (int i = 0; i<300; i++) head[i] = -1; tail = 0; for (int i = 1; i <= m; i++) { scanf("%d%d%d", &a, &b, &x); build(a, b, x); build(b, a, x); } scanf("%d%d", &S, &T); Dijkstra(); if (dis[T] != Max) printf("%d\n", dis[T]); else printf("-1\n"); } return 0; }
|